/**
  扩展一个test模块
**/
layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
}).extend({
    utilJs: 'lib/extend/utilJs', //主入口模块
}).define(['jquery', 'layer', 'utilJs', 'table', 'upload', 'form'], function (exports) { //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
    var $ = layui.$,
        t = layui.table,
        f = layui.form,
        u = layui.upload,
        utilJs = layui.utilJs,
        active = {
            add() {
                //                var checkStatus = t.checkStatus('LAY-user-back-manage')
                //let check = utilJs.admin.getCheck('#LAY-user-back-manage');
                utilJs.admin.prop('add_user.html', '新增用户', 1, () => {
                    let local = localStorage.getItem('guid')
                    if (local == null)
                        return false;
                    t.reload('LAY-user-back-manage', {
                        where: {} //设定异步数据接口的额外参数
                        //,height: 300
                    });
                })
            },
            batchdel() {
                var checkStatus = t.checkStatus('LAY-user-back-manage')
                if (checkStatus.data.length == 0) {
                    utilJs.admin.topMsg('请选择一条数据')
                    return false;
                }
                var ids = checkStatus.data.map(r => r.Id).toString();
                utilJs.admin.confim('确定执行此操作', cr => {
                    obj.delete_user(ids).then(ajax => {
                        utilJs.admin.topMsg('操作成功', () => {
                            t.reload('LAY-user-back-manage', {
                                where: {
                                    jsonStr: JSON.stringify(obj)
                                } //设定异步数据接口的额外参数
                                //,height: 300
                            });
                        })
                    })
                });
            }
        };
    //普通图片上传
    u.render({
        elem: '#test1',
        url: `${utilJs.api}UtilAPI/upload`,
        done: function (res) {
            $('#demo1').attr('src', res.data);
            $("#tx").val(res.data)
            utilJs.admin.topMsg('上传成功');
        },
        error: function () {
            utilJs.admin.topMsg('上传失败');
        }
    });
    //自定义验证规则
    f.verify({
        password: function (value) {
            if (value.length <= 6)
                return '密码必须大于六位数'
        },
    });
    f.on('submit(savaChange)', r => {
        let json = JSON.parse(localStorage.getItem('json'))
        let url = 'HLW01/add_users';
        let obj = r.field;
        delete obj.file;
        let newform = utilJs.like(obj)
        newform.roles = newform.X1;
        delete newform.X1
        if (json) {
            url = 'HLW01/update_user'
            newform.Id = json.Id;
        }
        utilJs.ajax(url, 'post', newform).then(ajax => {
            utilJs.admin.topMsg(ajax.msg, () => {
                localStorage.setItem('guid', utilJs.guid())                
                utilJs.admin.killIframe()
            })
        }).catch(ex => {})
    })
    f.on('submit(LAY-user-back-search)', r => {
        let obj = r.field;
        t.reload('LAY-user-back-manage', {
            where: {
                jsonStr: JSON.stringify(obj)
            } //设定异步数据接口的额外参数
            //,height: 300
        });
    })
    t.on('tool(LAY-user-back-manage)', r => {
        let event = r.event;
        let id = r.data.Id;
        switch (event) {
            case 'del':
                utilJs.admin.confim('删除后不可恢复哦！', cr => {
                    obj.delete_user(id).then(ajax => {
                        utilJs.admin.topMsg('操作成功')
                        r.del()
                    })
                });
                break;
            case 'edit':
                localStorage.setItem('json', JSON.stringify(r.data))
                utilJs.admin.prop('add_user.html', '用户修改', 1, () => {
                    let guid = localStorage.getItem('guid')
                    localStorage.removeItem('json')
                    localStorage.removeItem('guid')
                    if (guid != null) {
                        t.reload('LAY-user-back-manage', {
                            where: {} //设定异步数据接口的额外参数
                            //,height: 300
                        });
                    }
                })
                break;
        }
    })

    //LAY-user-back-manage
    t.render({
        elem: '#LAY-user-back-manage',
        headers: {
            Token: localStorage.getItem('Token')
        },
        url: `${utilJs.api}HLW01/get_users` //数据接口
            ,
        page: true //开启分页
            ,
        cols: [
            [ //表头
                {
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'IsDeal',
                    title: '状态',
                    width: 150,
                    toolbar: '#IsDeal'
                }, {
                    field: 'H3',
                    title: '头像',
                    width: 150,
                    toolbar: '#Img',
                },
                {
                    field: 'roles',
                    title: '角色',
                    width: 180,
                    style: 'color:red'
                },
                {
                    field: 'H1',
                    title: '用户名',
                    width: 150,
                }, {
                    field: 'H5',
                    title: '手机号码',
                    width: 150,
                },
                {
                    field: 'H6',
                    title: '邮箱',
                    width: 200,
                },
                {
                    field: 'H7',
                    title: '备注',
                    width: 250,
                },
                {
                    field: 'dtStr',
                    title: '创建时间',
                    width: 200,
                    sort: true,
                },

                {
                    fixed: 'right',
                    width: 150,
                    title: '操作',
                    align: 'center',
                    toolbar: '#barDemo'
                } //这里的toolbar值是模板元素的选择器
            ]
        ],
        done: () => {
            // localStorage.removeItem('guid')
            // layer.close(load)
            $('.tx').click(function () {
                let tx = $(this).attr('src')
                utilJs.admin.prop('/layuiadmin/lib/extend/photo.html?url=' + tx, '查看大图', ['400px', '400px'])
            });
            layui.define('form', () => {
                var f = layui.form;
                //监听指定开关
                f.on('switch(switchTest)', function (data) {
                    //utilJs.ajax('HLW01/update_state?uid='+)
                    //let check = utilJs.admin.getCheck('#LAY-user-back-manage');
                    let id = data.elem.dataset.id
                    utilJs.ajax('HLW01/update_state?uid=' + id, 'post', null).then(ajax => {
                        utilJs.admin.topMsg('操作成功')
                    })
                })             
            })
            localStorage.removeItem('guid')
            localStorage.removeItem('json')
        }
    })
    $('#util button').click(function (d) {
        let type = $(this).data('type')
        active[type] ? active[type].call(this) : '';
    })
    var obj = {
        /**
         * 初始化菜单
         * @param {ap地址} url 
         */
        start(url) {
            return utilJs.ajax(url, 'get', null);
        },
        get_roles(callback) {
            utilJs.ajax('HLW01/get_roles', 'get', null).then(ajax => {
                let data = ajax.data,
                    dom = '';
                // <input type="checkbox" name="like[write]" title="写作"></input>     
                data.map(r => {
                    dom += `<div class="layui-input-block">
                    <input type="checkbox" value="${r.Id}" name="like[${utilJs.guid()}]" title="${r.H1}"></input>
                    </div>`
                })
                $('#roles').append(dom)
                layui.define('form', () => {
                    var f = layui.form;
                    f.render()
                    callback();
                })
            })
        },
        delete_user(ids) {
            return utilJs.ajax('HLW01/delete_users?uids=' + ids, 'post', null)
        },
        edit_init() {
            let json = JSON.parse(localStorage.getItem('json'))
            utilJs.admin.set_form(f, 'example', json)
            $('#demo1').attr('src', $('#tx').val())
            $('#roles').children().map(function () {
                let t = $(this)[0].innerText;
                let obj = json;
                obj.roles.split(',').map(c => {
                    if (c == t) {
                        $(this).find('input').attr('checked', true)
                    }
                })
            })
            f.render()
        }
    };

    //输出test接口
    exports('_user', obj);
});